/**
 * 设备
 * Aaron create by 2019/06/14
 */
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  OneToMany,
} from 'typeorm';
import moment = require('moment');
import { User } from './user.entity';
import { Caller } from './caller.entity';

export enum Type {
  Gateway = 'gateway', // 智能网关
  Power = 'power', // 智慧电力
  Button = 'button', // 紧急按钮
  Environment = 'environment', // 环境检测
  Other = 'other', // 其他
}

export enum DeviceStatus {
  Online = 'online', // 在线
  Offline = 'offline', // 离线
}

@Entity()
export class Device {
  @PrimaryGeneratedColumn({
    comment: '自增ID',
  })
  id: number;

  @OneToMany((type) => Caller, (Caller) => Caller.device)
  caller: Caller[];

  @ManyToOne((type) => User, (User) => User.devices)
  user: User;

  @Column({
    type: 'int',
    nullable: true,
    comment: '用户id',
  })
  userId: number;

  @Column({
    nullable: true,
    comment: '用户名称',
    length: 20,
  })
  userName: string;

  @Column({
    nullable: true,
    comment: '设备状态',
    length: 10,
    default: 'offline',
  })
  status: DeviceStatus;

  @Column({
    nullable: true,
    comment: '设备编号',
    length: 50,
  })
  identifier: string;

  @Column({
    nullable: true,
    comment: 'IMEI',
    length: 50,
  })
  IMEI: string;

  @Column({
    nullable: true,
    comment: 'iccid',
    length: 50,
  })
  iccid: string;

  @Column({
    nullable: true,
    comment: '设备名称',
    length: 50,
  })
  name: string;

  @Column({
    nullable: true,
    comment: '产品类型',
    length: 50,
  })
  type: Type;

  @Column({
    nullable: true,
    comment: '断电告警通知',
    default: true,
  })
  outage: boolean;

  @Column({
    nullable: true,
    comment: '来电告警通知',
    default: true,
  })
  call: boolean;

  @Column({
    nullable: true,
    comment: '缺氧告警通知',
    default: true,
  })
  hypoxia: boolean;

  @Column({
    nullable: true,
    comment: '温度告警通知',
    default: true,
  })
  temperature: boolean;

  @Column({
    nullable: true,
    comment: '水浸告警通知',
    default: true,
  })
  flood: boolean;

  @Column({
    type: 'double',
    nullable: true,
    comment: '温度上限设置',
  })
  temperatureLimit: number;

  @Column({
    type: 'double',
    nullable: true,
    comment: '温度下限设置',
  })
  temperatureLower: number;

  @Column({
    type: 'double',
    nullable: true,
    comment: '湿度上限设置',
  })
  humidityLimit: number;

  @Column({
    type: 'double',
    nullable: true,
    comment: '湿度下限设置',
  })
  humidityLower: number;

  @Column({
    nullable: true,
    comment: '报警次数',
    default: 1,
  })
  frequency: number;

  @Column({
    nullable: true,
    comment: '间隔时间（分钟）',
    default: 10,
  })
  interval: number;

  @Column({
    nullable: false,
    comment: '是否删除',
    default: false,
  })
  isDel: boolean;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_electricity: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_oxygen: number;

  @Column({
    type: 'smallint',
    nullable: true,
    comment: '设备名称',
  })
  device_oxygen_info: number;

  @Column({
    type: 'smallint',
    nullable: true,
    comment: '设备名称',
  })
  device_water: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_three_electricity: number;

  @Column({
    type: 'float',
    nullable: true,
    comment: '设备名称',
  })
  device_temperature: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_humidity: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_standby: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_signal: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_message_switch: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_phone_switch: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_phone: number;

  @Column({
    type: 'tinyint',
    nullable: true,
    comment: '设备名称',
  })
  device_flood: number;

  @Column({
    nullable: true,
    comment: '所在位置',
  })
  position: string;

  @Column({
    nullable: true,
    comment: '详细地址',
  })
  address: string;

  @Column({
    nullable: true,
    comment: '安装位置',
  })
  installPosition: string;

  @Column({
    type: 'timestamp',
    nullable: true,
    comment: 'expireTime',
    update: false,
    transformer: {
      to: (value) => {
        return value;
      },
      from: (value) => {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },
    },
  })
  expireTime: Date;

  @CreateDateColumn({
    type: 'timestamp',
    nullable: false,
    comment: 'p_created_at',
    update: false,
    transformer: {
      to: (value) => {
        return value;
      },
      from: (value) => {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },
    },
  })
  p_created_at: Date;

  @CreateDateColumn({
    type: 'timestamp',
    nullable: false,
    comment: 'p_updated_at',
    update: true,
    transformer: {
      to: (value) => {
        return value;
      },
      from: (value) => {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },
    },
  })
  p_updated_at: Date;

  @CreateDateColumn({
    type: 'timestamp',
    nullable: true,
    comment: 'p_deleted_at',
    update: false,
    transformer: {
      to: (value) => {
        return value;
      },
      from: (value) => {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },
    },
  })
  p_deleted_at: Date;
}
